<!--
 * @Descripttion:
 * @version:
 * @Author: sueRimn
 * @Date: 2020-11-10 11:45:42
 * @LastEditors: sueRimn
 * @LastEditTime: 2020-11-13 11:50:18
-->
<template>
  <div class="app-container">
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item label="模块名称">
        <el-select v-model="value" placeholder="请选择" @change="optionChange">
          <el-option
            v-for="item in moduleList"
            :key="item.unid"
            :label="item.module_name"
            :value="item.unid"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="add_newAuth">新增页面权限</el-button>
      </el-form-item>
    </el-form>

    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>页面权限列表</span>
      </div>
      <div v-for="(item,index) in pageAuthList" :key="index" class="text item">
        <span>{{ item.auth_name }}</span>
        <div>
          <el-button type="text" @click="editAuth(item)">编辑</el-button>
          <!-- <el-button type="text" @click="delectAuth(item)">删除</el-button> -->
        </div>
      </div>
      <div v-if="pageAuthList.length == 0" class="noMore">--- 暂时无更多 ---</div>

    </el-card>
  </div>
</template>

<script>
import { get_moduleList, get_pageAuth, add_pageAuth, delect_pageAuth } from '@/api/module'
export default {
    name: 'PageAuth',
    data() {
        return {
            moduleList: [],
            value: '',
            pageAuthList: []
        }
    },
    created() {
        this.getModuleList()
    },
    methods: {
        // 拿到模块
        async getModuleList() {
            var dataList = await get_moduleList({})
            console.log('模块列表：', dataList)
            if (dataList.code == 200) {
                this.moduleList = dataList.data.list
            }
        },
        // 获取页面权限
        async optionChange() {
            var queryData = {
                module_id: this.value
            }
            var data = await get_pageAuth(queryData)
            if (data.code == 200) {
                this.pageAuthList = data.data.list
            } else {
                this.pageAuthList = []
            }
            console.log('获取页面权限', data)
        },
        add_newAuth() {
            if (this.value == '') {
                return this.$message.error('请先选择模块')
            }
            this.$prompt('请输入权限名', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                inputPattern: /\S/,
                inputErrorMessage: '权限名不能为空'
            }).then(({ value }) => {
                this.add_onePageAuth({
                    auth_name: value
                })
            }).catch()
        },
        // 新增模块页面权限
        async add_onePageAuth(obj) {
            console.log('auth_name', obj)
            var queryData = {
                ...obj,
                module_id: this.value
            }
            var data = await add_pageAuth(queryData)
            console.log('新增页面权限', data)
            if (data.code == 200) {
                this.$message({
                    message: '操作成功',
                    customClass: 'customClass',
                    type: 'success'
                })
                this.optionChange()
            }
        },
        delectAuth(item) {
            this.$confirm('此操作将删除该权限, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(async() => {
                var queryData = {
                    unid: item.unid
                }
                var data = await delect_pageAuth(queryData)
                console.log('删除', item, data)
                if (data.code == 200) {
                    this.$message({
                        type: 'success',
                        customClass: 'customClass',
                        message: '删除成功!'
                    })
                    this.optionChange()
                }
            }).catch(() => {})
        },
        editAuth(item) {
            console.log('编辑', item)
            this.$prompt('请输入权限名', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                inputPattern: /\S/,
                inputErrorMessage: '权限名不能为空'
            }).then(({ value }) => {
                var obj = {
                    unid: item.unid,
                    auth_name: value
                }
                this.add_onePageAuth(obj)
            }).catch()
        }
    }
}
</script>

<style scoped lang='scss'>
.text {
    font-size: 14px;
  }

  .item {
    margin-bottom: 18px;
    width: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
  }

  .clearfix:before,
  .clearfix:after {
    display: table;
    content: "";
  }
  .clearfix:after {
    clear: both
  }

  .box-card {
    width: 480px;
    margin-top: 20px;
  }

  .noMore {
      width: 100%;
      text-align: center;
  }
</style>
